package com.oocl.tm.main;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutput;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oocl.eventListener.RegisterWait;
import com.oocl.tm.log4j.Log4j;
import com.oocl.tm.protocol.MsgPackage;
import com.oocl.tm.protocol.Operation;
import com.oocl.util.ClientUtil;

public class Register extends JFrame {
	private static Register self;
	private JLabel nameLb;
	private JLabel psdLb;
	private JLabel psdCfLb;
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JPasswordField psdCf;
	private JButton register;
	private JButton cancelBtn;
	private JLabel title;
	private JFrame frame;

	private Register() {
		this.setAlwaysOnTop(true);
		this.setSize(350, 400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setTitle("Sign In");
		frame = this;

		init();
		addEvent();
		this.setVisible(true);
	}
	
	public static Register getRegister(){
		if(self == null){
			self = new Register();
		}
		return self;
	}

	public void init() {
		this.setLayout(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		psdCf = new JPasswordField();
		register = new JButton("log in");
		cancelBtn = new JButton("cancle");
		nameLb = new JLabel("Account:");
		psdLb = new JLabel("Password:");
		psdCfLb = new JLabel("Confirm:");
		title = new JLabel("TM Chat");
		title.setFont(new Font("Dialog", 1, 16));
		title.setBounds(135, 0, 70, 100);
		title.setForeground(Color.red);

		nameLb.setBounds(40, 90, 50, 25);
		psdCfLb.setBounds(45, 210, 50, 25);
		psdLb.setBounds(30, 150, 100, 25);
		nameTf.setBounds(100, 80, 200, 40);
		psdPf.setBounds(100, 140, 200, 40);
		psdCf.setBounds(100, 200, 200, 40);

		register.setBounds(60, 280, 90, 40);
		cancelBtn.setBounds(200, 280, 90, 40);
		this.add(psdCfLb);
		this.add(nameTf);
		this.add(psdPf);
		this.add(psdCf);

		this.add(cancelBtn);
		this.add(register);
		this.add(nameLb);
		this.add(psdLb);
		this.add(title);

	}

	public void addEvent() {
		cancelBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				frame.dispose();
			}
		});
		register.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String name = nameTf.getText();
				String pwd = new String(psdPf.getPassword());
				String cf = new String(psdCf.getPassword());
				if (name.equals("") || psdPf.getPassword().length==0 ||psdCf.getPassword().length == 0) {
					JOptionPane.showConfirmDialog(null, "Acount/Password is null!", "WARNING", JOptionPane.DEFAULT_OPTION);
				}else if(!pwd.equals(cf)){
					JOptionPane.showConfirmDialog(null, "Confirm/Password is different", "WARNING", JOptionPane.DEFAULT_OPTION);
					psdPf.setText("");
					psdCf.setText("");
				}else{
					Future<String> future = null;
					ExecutorService es = null;
					Socket client = null;
					String result = null;
					try {				
						client = new Socket("10.222.29.165", 8889);
						es = Executors.newFixedThreadPool(1);
						future = es.submit(new RegisterWait(name, pwd, client));
						result = future.get(3, TimeUnit.SECONDS);
						MsgPackage msg = ClientUtil.toMsg(result);
						switch (msg.getOperation()) {
						case NOTIFY:
							JOptionPane.showConfirmDialog(null, "Successful", "Congratulate", JOptionPane.DEFAULT_OPTION);
							frame.dispose();
							break;
						case REGISTER:
							JOptionPane.showConfirmDialog(null, "User Name repeat", "Warning", JOptionPane.DEFAULT_OPTION);
							psdPf.setText("");
							psdCf.setText("");
							break;
						default:
							break;
						}
					} catch (InterruptedException e1) {
						future.cancel(true);
					} catch (ExecutionException e1) {
						future.cancel(true);
					} catch (TimeoutException e1) {
						JOptionPane.showConfirmDialog(null, "TimeOut!", "WARNING", JOptionPane.DEFAULT_OPTION);
						psdPf.setText("");
						psdCf.setText("");
						future.cancel(true);
						es.shutdown();
						return;
					} catch (UnknownHostException e1) {
						e1.printStackTrace();
					} catch (IOException e1) {
						Log4j.getLogger().info("无法连接服务器......");
					}finally{
						try {
							if(client != null){
								client.close();
							}
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
					
					
				}

			}
		});
		
		this.addWindowListener(new WindowAdapter() {
			public synchronized void windowClosing(WindowEvent e) {
				self = null;
				frame.dispose();
			}
		});

	}

	public static void main(String[] args) {
		new Register();
	}

}
